package slu.appli.wodtimer.info;

import java.util.ArrayList;
import java.util.List;

import slu.appli.tools.parser.stringparser.interfaces.model.ModelReaderInterface;

public class WodTimerInformationControler {
	private long startTime = 0;
	private long elapseStartTime = 0;
	private long elapseDuration = 0;
	private long elapse = 0;
	private long duration = 0;
	private long realDuration = 0;
	private long pauseDuration = 0;
	private long pauseTime = 0;
	private int state = WodTimerInformation.STATE_END;
	private ModelReaderInterface reader = null;
	private final List<WodTimerInformationListenerInterface> listeners;
	
	public WodTimerInformationControler() {
		super();
		this.listeners = new ArrayList<WodTimerInformationListenerInterface>();
	}
	public void addListener(WodTimerInformationListenerInterface listener) {
		this.listeners.add(listener);
	}
	public void fire() {
		WodTimerInformation infos = new WodTimerInformation(this.startTime,
				this.elapseStartTime,
				this.elapseDuration,
				this.elapse,
				this.duration,
				this.realDuration,
				this.pauseDuration,
				this.state,
				this.reader);
		for (WodTimerInformationListenerInterface listener : this.listeners) {
			listener.WodTimerChanged(infos);
		}
	}
	public void end() {
		this.state = WodTimerInformation.STATE_END;
		this.fire();
	}
	public long getDuration() {
		return this.duration;
	}
	public long getElapse() {
		return this.elapse;
	}
	public long getElapseDuration() {
		return this.elapseDuration;
	}
	public long getElapseTime() {
		return this.elapseStartTime;
	}
	public long getPauseDuration() {
		return this.pauseDuration;
	}
	public ModelReaderInterface getReader() {
		return reader;
	}
	public long getRealDuration() {
		return this.realDuration;
	}

	public long getStartTime() {
		return this.startTime;
	}
	public int getState() {
		return this.state;
	}
	public void markException() {
		this.state = WodTimerInformation.STATE_ERROR;
		this.fire();
	}
	public void markDuration() {
		this.state = WodTimerInformation.STATE_ELAPSE;
		this.duration = System.currentTimeMillis() - this.startTime - this.pauseDuration;
		this.realDuration = System.currentTimeMillis() - this.startTime;
		this.fire();
	}
	public void markElapse() {
		this.elapse = System.currentTimeMillis() - this.elapseStartTime;
		if (this.elapse > this.elapseDuration) this.elapse = this.elapseDuration;
		this.markDuration();
	}
	public void removeListener(WodTimerInformationListenerInterface listener) {
		this.listeners.remove(listener);
	}
	public long start(ModelReaderInterface reader) {
		this.state = WodTimerInformation.STATE_START;
		this.elapseStartTime = 0;
		this.elapseDuration = 0;
		this.elapse = 0;
		this.duration = 0;
		this.realDuration = 0;
		this.pauseDuration = 0;
		this.pauseTime = 0;
		this.startTime = System.currentTimeMillis();
		this.reader = reader;
		this.fire();
		return this.startTime;
	}
	public long startElapseTime(long elapseDuration) {
		this.state = WodTimerInformation.STATE_NEW_ELAPSE;
		this.elapseStartTime = System.currentTimeMillis();
		this.elapseDuration = elapseDuration;
		this.fire();
		return this.elapseStartTime;
	}
	public long startPause() {
		this.state = WodTimerInformation.STATE_PAUSE;
		this.pauseTime = this.elapseStartTime;
		this.fire();
		return this.pauseTime;
	}
	public long stopPause() {
		this.state = WodTimerInformation.STATE_RESTART;
		if (this.pauseTime != 0) {
			this.pauseDuration += System.currentTimeMillis() - this.pauseTime;
			this.pauseTime = 0;
		}
		this.fire();
		return this.pauseDuration;
	}
	
}

